/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.panel;

import java.awt.CardLayout;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.print.PrinterException;

import javax.swing.JPanel;

import org.jxUtils.system.PrintUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.panel
 * @date 19/06/2014 - 08:35:44
 */
public class CardPanel extends JPanel {
	
	private static final long serialVersionUID = -7634794507600282755L;
	
	/**
	 * 
	 */
	public CardPanel() {
	
		super(new CardLayout());
	}
	
	/**
	 * Flips to the first card of the container.
	 * 
	 * @see java.awt.CardLayout#last
	 */
	public final void first() {
	
		getLayout().first(this);
	}
	
	/**
	 * @param name
	 * @return visible {@link Component} or null
	 */
	public final Component getComponent(final String name) {
	
		synchronized(getTreeLock()) {
			
			//
			for(Component comp : getComponents()) {
				
				//
				if(comp != null && comp.getName() != null && comp.getName().equals(name)) {
					
					return comp;
				}
			}
			
			return null;
		}
	}
	
	/**
	 * @return visible {@link Component} or null
	 */
	public final Component getCurrent() {
	
		synchronized(getTreeLock()) {
			
			//
			for(Component comp : getComponents()) {
				
				//
				if(comp != null && comp.isVisible()) {
					
					return comp;
				}
			}
			
			return null;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Container#getLayout()
	 */
	@Override
	public final CardLayout getLayout() {
	
		return (CardLayout)super.getLayout();
	}
	
	/**
	 * Flips to the last card of the container.
	 * 
	 * @see java.awt.CardLayout#first
	 */
	public final void last() {
	
		getLayout().last(this);
	}
	
	/**
	 * Flips to the next card of the specified container. If the
	 * currently visible card is the last one, this method flips to the
	 * first card in the layout.
	 * 
	 * @see java.awt.CardLayout#previous
	 */
	public final void next() {
	
		getLayout().next(this);
	}
	
	/**
	 * Flips to the previous card of the specified container. If the
	 * currently visible card is the first one, this method flips to the
	 * last card in the layout.
	 * 
	 * @see java.awt.CardLayout#next
	 */
	public final void previous() {
	
		getLayout().previous(this);
	}
	
	/**
	 * @throws PrinterException 
	 */
	public final void printCurrent() throws PrinterException {
	
		PrintUtil.print(getCurrent());
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Container#setLayout(java.awt.LayoutManager)
	 */
	@Override
	public final void setLayout(final LayoutManager mgr) {
	
		//
		if( !(mgr instanceof CardLayout)) {
			
			return;
		}
		
		super.setLayout(mgr);
	}
	
	/**
	 * Flips to the component that was added to this layout with the
	 * specified <code>name</code>, using <code>addLayoutComponent</code>.
	 * If no such component exists, then nothing happens.
	 * 
	 * @param name
	 *        the component name
	 * @see java.awt.CardLayout#addLayoutComponent(java.awt.Component, java.lang.Object)
	 */
	public final void show(final String name) {
	
		getLayout().show(this, name);
	}
}
